package com.samehope.ar.controller;


import com.samehope.ar.common.CommonResult;
import com.samehope.ar.dto.ItemTypeAddParam;
import com.samehope.ar.dto.ItemTypeUpdateParam;
import com.samehope.ar.service.IPatrolItemTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 巡检项分类表 前端控制器
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@RestController
@RequestMapping("/itemType")
public class PatrolItemTypeController {

    @Autowired
    private IPatrolItemTypeService itemTypeService;

    /**
     * 新增巡检项类型
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PostMapping("/add")
    public CommonResult add(@RequestBody @Valid ItemTypeAddParam param) {
        itemTypeService.add(param);
        return CommonResult.success(null);
    }

    /**
     * 删除巡检项分类
     * @param itemTypeId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @DeleteMapping("/delete/{itemTypeId}")
    public CommonResult delete(@PathVariable("itemTypeId") Long itemTypeId) {
        itemTypeService.delete(itemTypeId);
        return CommonResult.success(null);
    }

    /**
     * 更新巡检项类型
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PutMapping("/update")
    public CommonResult update(@RequestBody @Valid ItemTypeUpdateParam param) {
        itemTypeService.update(param);
        return CommonResult.success(null);
    }

    /**
     * 巡检项分类树结构
     * @param condition
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/tree")
    public CommonResult tree(@RequestParam(required = false) String condition) {
        return CommonResult.success(itemTypeService.tree(condition));
    }

    /**
     * 巡检项分类下拉选视图对象集合
     * @return
     */
    @GetMapping("/typesForSelect")
    public CommonResult typesForSelect() {
        return CommonResult.success(itemTypeService.typesForSelect());
    }
}
